package cz.mff.curreco.utils;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cz.mff.curreco.model.MoneyItem;


public class MyDomTransformer 
{
	public void transform (Document xmlDocument, MoneyItem newItem) 
	{
		NodeList coinRoot = xmlDocument.getElementsByTagName("coins");
		Node root = coinRoot.item(0);
		
		
		
		Element coin = xmlDocument.createElement("coin");
		
		Attr fullName = xmlDocument.createAttribute("officialName");
		fullName.setValue(newItem.getOfficialName());
		Attr amount = xmlDocument.createAttribute("amount");
		amount.setValue("" + newItem.getAmount());
		Attr countryCode = xmlDocument.createAttribute("countryCode");
		countryCode.setValue(newItem.getCountryCode());
		Attr currencySymbol = xmlDocument.createAttribute("currencySymbol");
		currencySymbol.setValue(newItem.getCurrencySymbol());
		Attr id = xmlDocument.createAttribute("id");
		id.setValue("" + newItem.getId());
		
		coin.setAttributeNode(amount);
		coin.setAttributeNode(countryCode);
		coin.setAttributeNode(currencySymbol);
		coin.setAttributeNode(id);
		coin.setAttributeNode(fullName);
		
		Element pictureFrontSource = xmlDocument.createElement("pictureFrontSource");
		Attr frontAttr = xmlDocument.createAttribute("path");
		frontAttr.setValue(newItem.getPictureFrontPath());
		pictureFrontSource.setAttributeNode(frontAttr);
		coin.appendChild(pictureFrontSource);		
		
		Element pictureBackSource = xmlDocument.createElement("pictureBackSource");
		Attr backAttr = xmlDocument.createAttribute("path");
		backAttr.setValue(newItem.getPictureBackPath());
		pictureBackSource.setAttributeNode(backAttr);
		coin.appendChild(pictureBackSource);
		
		Element circularDescriptor = xmlDocument.createElement("circularDescriptor");
		circularDescriptor.appendChild(xmlDocument.createTextNode(newItem.getCircularDescriptor()));
		coin.appendChild(circularDescriptor);	
		
		Element fatCircularDescriptor = xmlDocument.createElement("fatCircularDescriptor");
		fatCircularDescriptor.appendChild(xmlDocument.createTextNode(newItem.getFatCircularDescriptor()));
		coin.appendChild(fatCircularDescriptor);
		
		
		root.appendChild(coin);
		
		
		
	}
}